<?php
class UsersController extends AppController {

	var $name = 'Users';
	
	function admin_login() {
		App::import('Component', 'Auth');
		if(!empty($this->data)){
			$auth = new AuthComponent();
			$this->data['User']['password'] = $auth->password($this->data['User']['password']);
			$this->User->recursive = -1;
			$result = $this->User->find('all', array('conditions' => array('username' => $this->data['User']['username'], 'password' => $this->data['User']['password'] )));
			if(!empty($result)){
				$this->Session->write('User.id', $result[0]['User']['id']);
				$this->Session->write('User.username', $result[0]['User']['username']);
				$this->Session->write('User.email', $result[0]['User']['email']);
				$this->Session->write('User.gender', $result[0]['User']['gender']);
				$this->Session->write('User.phone', $result[0]['User']['phone']);
				$this->redirect(array('action' => 'index'));
			}
			$this->data['User']['password'] = '';
			$this->Session->setFlash(__('Invalid username or password !', true));
		}

	}
	
	function admin_logout() {
		$this->Session->destroy();
		$this->redirect(array('controller' => 'users', 'action' => 'login'));
	}

	function admin_index() {
		$this->_check();
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
		$this->_check();
		if (!empty($this->data)) {
			App::import('Component', 'Auth');
			$auth = new AuthComponent();
			$this->data['User']['password'] = $auth->password($this->data['User']['password']);
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function admin_edit($id = null) {
		$this->_check();
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			App::import('Component', 'Auth');
			$auth = new AuthComponent();
			$this->data['User']['password'] = $auth->password($this->data['User']['password']);
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function admin_delete($id = null) {
		$this->_check();
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>